import { queryAnnounce, submitFeedback } from '../services/global';
import { parse } from 'qs';
import cookie from 'cookie-monster';

export default {
  namespace: 'global',

  state: {
    message: {
      key: Symbol('msg_global'), // 确保每次message的key独一无二
      type: '',
      msg: ''
    },
    announce: [],
    loading: false,
    shouldUpdate: false,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === '/') {
          dispatch({
            type: 'fetchAnnounce'
          });
        } else if (location.pathname === '/logout') {
          if (cookie.getItem(OAglobal.tokenName)) {
            dispatch({
              type: 'logOut'
            });
          } else {
            window.location.href = '/login';
          }
        }
      });
    },
  },

  effects: {
    *logOut() {
      // 注销登录
      cookie.removeItem(OAglobal.tokenName);
      setTimeout(function() {
        window.location.reload();
      }, 100);
    },

    *submitFeedback({ payload }, { call, put }) {
      console.log('payload', payload);
      const {data} = yield call(submitFeedback, parse(payload));
      if (data) {
        if (data.success == 'true') {
          yield put({
            type: 'showMsg',
            payload: {
              msgType: 'success',
              msg: data.data,
              shouldUpdate: true
            }
          });
        } else {
          yield put({
            type: 'showMsg',
            payload: {
              msgType: 'error',
              msg: data.data
            }
          })
        }
      } else {
        yield put({
          type: 'showMsg',
          payload: {
            msgType: 'error',
            msg: '网络拥堵，更新失败了'
          }
        })
      }
    },

    // 获取数据
    *fetchAnnounce({ payload }, { call, put }) {
      yield put({ type: 'showLoading' });
      const {data} = yield call(queryAnnounce, parse(payload));
      if (data) {
        if (data.success == 'true') {
          yield put({
            type: 'querySuccess',
            payload: {
              announce: data.data
            },
          })
        } else {
          yield put({
            type: 'showMsg',
            payload: {
              msgType: 'error',
              msg: data.data
            }
          })
        }
      } else {
        yield put({
          type: 'showMsg',
          payload: {
            msgType: 'error',
            msg: '啊哦，获取数据失败，可能是网络问题'
          }
        })
      }
    },
  },

  reducers: {
    querySuccess(state, action) {
      const params = {
        shouldUpdate: true,
        loading: false,
        ...action.payload
      }
      return { ...state, ...params };
    },

    showLoading(state) {
      const params = {
        shouldUpdate: true,
        loading: true
      }
      return { ...state, ...params };
    },

    showMsg(state, action) {
      const params = action.payload.shouldUpdate
                       ? {
                           message: {
                             key: Symbol('msg_global'),
                             type: action.payload.msgType,
                             msg: action.payload.msg
                           },
                           ...action.payload
                         }
                       : {
                           shouldUpdate: false,
                           message: {
                             key: Symbol('msg_global'),
                             type: action.payload.msgType,
                             msg: action.payload.msg
                           }
                         }
      return { ...state, ...params};
    }
  }

}
